package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.model.Article;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.ArticleService;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${lyh} on
 * 2018/8/24.
 *
 * 文章管理类
 */

@RestController
public class ArticleController {

    @Autowired
     private ArticleService articleService;

     /*
     * 分页查询-------------新闻列表
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time
     * @param way desc，asc，noway
     */

    //@GetMapping("/article/list/{page}/{size}/{search}/{way}/{articleCategoryId}")
    @GetMapping("/article/list")
    public Map<String, Object> list(@RequestParam(value = "page") Integer page,
                                    @RequestParam(value = "size") Integer size,
                                    @RequestParam(value ="search") String search,
                                    @RequestParam(value ="way") String way,
                                    @RequestParam(value ="articleCategoryId", required = false) String categoryId){
        Map<String, Object> map = new HashMap<>();
         PageHelper.startPage(page, size);
        List<Article> list = articleService.selectAllByArticle(0,search, way, categoryId);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("pages", pageInfo.getPages());
            map.put("status",200);
        }else{
            map.put("status",401);
            map.put("msg","暂无数据");
        }
        return map;
    }


    /**
     * 查询新闻详情
     * @param articleId:新闻信息的id
     * @return
     */
    @GetMapping("/article/getArticleById/{articleId}")
    public Response getArticleById(@PathVariable("articleId")  String articleId){
        return articleService.getArticleById(articleId);
    }

    /**
     * 文章的修改
     * @param article
     * @return
     */
    @PostMapping("/article/updateArticle")
    public Response updateArticle(Article article){
        return articleService.updateArticle(article);
    }

    /**
     * 文章的保存
     * @param article
     * @return
     */
    @PostMapping("/article/save")
    public Response save(Article article){
        return articleService.save(article);
    }

    /**
     * 文章的删除
     * @param articleId
     * @return
     */
    @GetMapping("/article/delete/{articleId}")
    public Response delete(@PathVariable("articleId") String articleId){
        return articleService.delete(articleId);
    }

    /**
     * 文章分类列表
     * @return
     */
    @GetMapping("/article/articleCategoryList")
    public Response articleCategoryList(){
        return articleService.articleCategoryList();
    }









}
